package com.ht.modules.ai.company.service.impl;

import com.ht.modules.authentication.utils.AesUtil;
import com.ht.modules.authentication.utils.RsaUtil;
import com.ht.utils.DicConstants;
import com.ht.modules.ai.company.entity.Company;
import com.ht.modules.ai.company.entity.CompanyVo;
import com.ht.modules.ai.company.mapper.CompanyMapper;
import com.ht.modules.ai.company.service.ICompanyService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

import static com.ht.modules.authentication.utils.RsaUtil.getPublicPrivateKey;

/**
 * 企业信息
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 分页列表查询
     *
     * @param companyVo 参数
     * @param page      分页对象
     * @param column    默认排序列
     * @param order     默认排序方式
     * @return 分页列表
     */
    @Override
    public Page<CompanyVo> list(Page<CompanyVo> page, CompanyVo companyVo, String column, String order) {
        return page.setRecords(companyMapper.list(page, companyVo, column, order));
    }

    /**
     * 不分页列表查询
     *
     * @param companyVo 参数
     * @param column    默认排序列
     * @param order     默认排序方式
     * @return 不分页列表
     */
    @Override
    public List<CompanyVo> listNoPage(CompanyVo companyVo, String column, String order) {
        return companyMapper.list(null, companyVo, column, order);
    }

    /**
     * 添加
     *
     * @param company 参数
     */
    @Override
    public void add(Company company) {
        company.setDelFlag(DicConstants.DEL_FLAG_WSC);
        String publicAesKey = AesUtil.getKey();
        String privateAesKey = AesUtil.getKey();
        Map<String, Object> KeyPair = RsaUtil.genKeyPair(publicAesKey, privateAesKey);
        company.setPrivateKey(getPublicPrivateKey(KeyPair, privateAesKey));
        company.setPublicKey(getPublicPrivateKey(KeyPair, publicAesKey));
        this.save(company);
    }

    /**
     * 编辑
     *
     * @param company 参数
     */
    @Override
    public void edit(Company company) {
        Company companyEntity = this.getById(company.getId());
        if (null == companyEntity) {
            throw new RuntimeException("未找到对应实体！");
        }
        this.updateById(company);
    }

    /**
     * 通过id删除（逻辑删除）
     *
     * @param id 参数
     */
    @Override
    public void delete(Integer id) {
        Company company = this.getById(id);
        if (null == company) {
            throw new RuntimeException("未找到对应实体！");
        }
        company.setDelFlag(DicConstants.DEL_FLAG_YSC);
        this.updateById(company);
    }

    /**
     * 批量删除（逻辑删除）
     *
     * @param ids 参数
     */
    @Override
    public void deleteBatch(String ids) {
        if (Strings.isBlank(ids)) {
            throw new RuntimeException("参数不识别！");
        }
        companyMapper.updateBatchByIds("del_flag", DicConstants.DEL_FLAG_YSC, ids.split(","));
    }

    /**
     * 通过id查询
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public Company queryById(Integer id) {
        Company company = this.getById(id);
        if (null == company) {
            throw new RuntimeException("未找到对应实体！");
        }
        return company;
    }

    /**
     * 通过id查询Vo
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public CompanyVo queryVoById(Integer id) {
        CompanyVo companyVo = companyMapper.getVoById(id);
        if (null == companyVo) {
            throw new RuntimeException("未找到对应实体！");
        }
        return companyVo;
    }

    /**
     * 通过excel导入数据
     *
     * @param importList 参数
     */
    @Override
    public void importExcel(List<Company> importList) {
        if (importList.isEmpty()) {
            return;
        }
        for (Company eMap : importList) {
            eMap.setDelFlag(DicConstants.DEL_FLAG_WSC);
        }
        this.saveBatch(importList);
    }

}
